package com.api.phm.domain.vo;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.api.common.config.ApiConfig;
import com.api.common.utils.DateUtils;
import com.api.phm.domain.query.alarmRule.AlarmRuleQuery;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
@Setter
public class AlarmLogVo {
    private Long alarmId;
    private Long ruleId;
    private JSONObject localRuleName;
    private String ruleName;
    private Long plcId;
    private JSONObject localPlcName;
    private String plcName;
    private Long lineId;
    private JSONObject localLineName;
    private String lineName;
    private Long robotId;
    private JSONObject localRobotName;
    private String robotName;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;
    private int alarmLevel;
    private String alarmStatus;
    private String alarmMessage;
    private JSONObject trigger;
    private String triggerVal = "Null";
    private String readMark;
    private String now = DateUtils.getTime();
    private int pageNum;
    private int pageSize;

    public AlarmLogVo() {

    }

    public AlarmLogVo(AlarmRuleQuery alarmRuleQuery, Integer alarmLevel) {
        ruleToLog(alarmRuleQuery, alarmLevel);
    }

    //public void initControl()
    public void setTrigger(JSONObject trigger) {
        this.trigger = trigger;
        this.triggerVal = trigger.toString();
    }

    /**
     * 告警信息录入
     *
     * @param alarmRuleQuery
     * @param level
     */
    private void mkAlarmMessage(AlarmRuleQuery alarmRuleQuery, Integer level) {
        String alarmMess = this.alarmMessage;
        Map<String, String> map = new HashMap<>();
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                String val = String.valueOf(field.get(this));
                String strLabel = "${" + field.getName() + "}";
                val = val.equals("null") ? "" : val;
                map.put(strLabel, val);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.setAlarmMessage(parse(alarmMess, map));
    }

    /**
     * 告警信息元素插入
     *
     * @param mess
     * @param map
     * @return
     */
    private String parse(String mess, Map<String, String> map) {
        String pattern = "\\$\\{(.*?)}";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(mess);
        StringBuffer sr = new StringBuffer();
        System.out.println("[map]:" + map.toString());
        while (m.find()) {
            String group = m.group();
            System.out.println("[group]:" + group);
            if (map.containsKey(group)) {
                System.out.println("Map item:" + map.get(group));
                m.appendReplacement(sr, map.get(group));
            }
        }
        m.appendTail(sr);
        return sr.toString();
    }

    public void ruleToLog(AlarmRuleQuery alarmRuleQuery, Integer alarmLevel) {
        String lang = ApiConfig.getInitLang();
        this.ruleId = alarmRuleQuery.getRuleId();
        this.localRuleName = alarmRuleQuery.getLocalRuleName();
        System.out.println(localRuleName);
        this.ruleName = alarmRuleQuery.getLocalRuleName().getString(lang);
        this.plcId = alarmRuleQuery.getPlcId();
        this.localPlcName = alarmRuleQuery.getLocalPlcName();
        this.plcName = alarmRuleQuery.getLocalPlcName().getString(lang);
        this.lineId = alarmRuleQuery.getLineId();
        this.localLineName = alarmRuleQuery.getLocalLineName();
        this.lineName = alarmRuleQuery.getLocalLineName().getString(lang);
        this.robotId = alarmRuleQuery.getRobotId();
        this.localRobotName = alarmRuleQuery.getLocalRobotName();
        this.robotName = alarmRuleQuery.getLocalRobotName().getString(lang);
        this.startTime = DateUtils.getNowDate();
        this.alarmLevel = alarmLevel;
        this.alarmStatus = alarmLevel == 0 ? "RES" : "ALM";
        this.alarmMessage = alarmRuleQuery.getAlarmMessage();
        mkAlarmMessage(alarmRuleQuery, alarmLevel);
        this.readMark = "N";
    }

    public void ruleToLog(AlarmRuleQuery alarmRuleQuery, JSONObject trigger, Integer alarmLevel) {
        String lang = ApiConfig.getInitLang();
        this.ruleId = alarmRuleQuery.getRuleId();
        this.plcId = alarmRuleQuery.getPlcId();
        this.lineId = alarmRuleQuery.getLineId();
        this.startTime = DateUtils.getNowDate();
        this.alarmLevel = alarmLevel;
        this.alarmStatus = alarmLevel == 0 ? "RES" : "ALM";
        setTrigger(trigger);
        this.readMark = "N";
    }
}
